<template>
	<view class="content">
		<uni-table border stripe emptyText="暂无更多数据">
			<!-- 表头行 -->
			<uni-tr>
				<uni-th align="center" style="white-space:nowrap;">序号</uni-th>
				<!-- <uni-th align="center">ID</uni-th> -->
				<uni-th align="center">位置</uni-th>
				<uni-th align="center">{{type == $enums.DataEntry_Type.断丝?'断丝数':"测量值"}}</uni-th>
				<uni-th v-if="isEdit" align="center">操作</uni-th>
			</uni-tr>
			<!-- 表格数据行 -->
			<uni-tr v-for="(item,index) in list">
				<uni-td align="center">{{index+1}}</uni-td>
				<!-- <uni-td>{{item.autoID}}</uni-td> -->
				<uni-td>
					<u-input type="number" :formatter="$gx.inputFormatter" v-model="item.position" placeholder="" :disabled="!isEdit">
						<u--text text="m" slot="suffix" type="tips"></u--text>
					</u-input>
				</uni-td>
				<uni-td>
					<u-input type="number" :formatter="(e) => {return $gx.inputFormatter(e, 0);}" v-model="item.num" placeholder="" :disabled="!isEdit" v-if="type == $enums.DataEntry_Type.断丝"></u-input>
					<u-input type="number" :formatter="$gx.inputFormatter" v-model="item.num" placeholder="" :disabled="!isEdit" v-if="type == $enums.DataEntry_Type.直径">
						<u--text text="mm" slot="suffix" type="tips"></u--text>
					</u-input>
				</uni-td>
				<uni-td v-if="isEdit">
					<u-button type="error" size="small" @click="delRow(item,index)" text="删除"></u-button>
				</uni-td>
			</uni-tr>

		</uni-table>
		<view class="mart-30" v-if="isEdit">
			<u-row justify="center" gutter="10">
				<u-col span="3">
					<u-button type="primary" size="small" @click="addRow" text="添加"></u-button>
				</u-col>
				<u-col span="3">
					<u-button type="success" size="small" @click="onSave" text="提交"></u-button>
				</u-col>
			</u-row>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				title: "",
				isEdit: false,
				usingData: {},
				wireIndex:null,
				type:null
			}
		},
		//生命周期-加载
		onLoad: function(option) {
			let that = this;
			// #ifdef APP-NVUE
			const eventChannel = this.$scope.eventChannel; // 兼容APP-NVUE
			// #endif

			// #ifndef APP-NVUE
			const eventChannel = this.getOpenerEventChannel();
			// #endif
			eventChannel.on('ropeData', function(data) {
				that.init(data.data);
				if(data.data.isLook){return;}
				let {
					isEdit,
					type,
					usingData,
					wireIndex
				} = {
					...data.data
				};
				that.type = type
				that.isEdit = isEdit;
				that.wireIndex=wireIndex;
				usingData.type = type;
				that.usingData = usingData;
			})
		},
		methods: {
			addRow() {
				let {
					type,
					usingWireropeID
				} = {
					...this.usingData
				}
				this.list.push({
					type: type,
					usingWireropeID: usingWireropeID,
					position: null,
					num: null,
				})
			},
			delRow(row,index){
				this.list.splice(index,1);
			},
			onSave() {
				if(!this.list.length){
					uni.navigateBack({
					    delta: 1
					});
					return;
				}
				let {installLengthNum,type}={...this.usingData};
				for (let i = 0; i < this.list.length; i++) {
					let item = this.list[i];
					if (this.$gx.IsEmpty(item.position)) {
						this.$refs.uToast.show({
						    type: "warning",
						    title: "",
						    message: "请完善序号【" + (Number(i) + 1) + "】位置信息！"
						});
						return;
					}
					if (Number(item.position) > Number(installLengthNum)) {
						this.$refs.uToast.show({
						    type: "warning",
						    title: "",
						    message:"序号【" + (Number(i) + 1) + "】的位置信息必须小于安装长度" + installLengthNum + "米！"
						});
						return;
					}
					if (this.$gx.IsEmpty(item.num)) {
						if (type == this.$enums.DataEntry_Type.断丝) {
							this.$refs.uToast.show({
							    type: "warning",
							    title: "",
							    message:"请完善序号【" + (Number(i) + 1) + "】断丝数！"
							});
						} else {
							this.$refs.uToast.show({
							    type: "warning",
							    title: "",
							    message:"请完善序号【" + (Number(i) + 1) + "】测量值！"
							});
						}
						return;
					}
					if (type == this.$enums.DataEntry_Type.断丝) {
						let nextItem = this.list[Number(i) + 1];
						if (Number(i) + 1 < this.list.length) {
							if (Number(item.position) > Number(nextItem.position)) {
								this.$refs.uToast.show({
								    type: "warning",
								    title: "",
								    message: "序号【" + (Number(i) + 1) + "】的位置信息必须小于序号【" + (Number(i) + 2) + "】的位置信息！"
								});
								return;
							}
							// 判断当前位置+捻距是否超过下一位置
							if (Number(item.position) + Number(this.usingData.nianJu) / 100 >= Number(nextItem
									.position)) {
								item.sum = Number(item.num) + Number(nextItem.num);
								item.dsEnd = nextItem.position;
								item.dsStart = item.position;
							} else {
								item.sum = Number(item.num);
								item.dsStart = item.position;
								item.dsEnd = item.position;
							}
						} else {
							item.sum = Number(item.num);
							item.dsStart = item.position;
							item.dsEnd = item.position;
						}
					}
				}

				if (type == this.$enums.DataEntry_Type.断丝) {
					let maxData = this.$_.maxBy(this.list, (o) => {
						return o.sum;
					});
					let positionArr = this.$_.filter(this.list, function(o) {
						return o.sum == maxData.sum;
					});
					// emits("close", list.value, positionArr, maxData.sum, type.value, wireIndex.value);
					uni.$emit("siteDataOperReturn", {
						list:this.list,
						position:positionArr,
						maxSum:maxData.sum,
						type:type,
						wireIndex:this.wireIndex
					});
				} else if (type == this.$enums.DataEntry_Type.直径) {
					let minData = this.$_.minBy(this.list, (o) => {
						return o.num;
					});

					let zjDia = ((1 - minData.num / Number(this.usingData.diameter)) * 100).toFixed(2);
					// emits("close", list.value, minData.position, zjDia, type.value, wireIndex.value);
					 uni.$emit("siteDataOperReturn", {
						list:this.list,
						position:minData.position,
						maxSum:zjDia,
						type:type,
						wireIndex:this.wireIndex
					});
				}
				uni.navigateBack({
				    delta: 1
				});
			},
			init(allData) {
				let {
					name,
					data,
					type
				} = {
					...allData
				};
				let str = type == 1 ? "断丝" : "直径";
				this.title = str + "数据录入";
				if(name){
					this.title += ' - '+name
				}
				uni.setNavigationBarTitle({
					title: `${this.title}`
				}); //设置标题
				this.list = this.$_.cloneDeep(data);
			},
		}
	}
</script>

<style scoped>
	.content {
		box-sizing: border-box;
		padding-top: 30rpx;
	}

	.content /deep/ .uni-table {
		min-width: auto !important;
	}
</style>